package com.personal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personal.mapper.*;
import com.personal.pojo.*;
import com.personal.service.MemberService;
import com.personal.utils.GetAWeekCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImp implements MemberService {
    @Autowired
    private TMemberMapper memberMapper;

    @Autowired
    private TCoachMapper coachMapper;

    @Autowired
    private TCourseMapper courseMapper;

    @Autowired
    private TApplyMapper applyMapper;

    @Autowired
    private TReserveMapper reserveMapper;

    //1.登录方法
    @Override
    public TMember login(String name, String pass) {
        return memberMapper.selectByNamePass(name,pass);
    }

    //2.注册方法
    @Override
    public int register(TMember member) {
        return memberMapper.register(member);
    }

    //2.1根据登录名查
    @Override
    public List findByName(String username) {
        return memberMapper.selectByName(username);
    }

    @Override
    public int editByKey(TMember member) {
        return memberMapper.updateByPrimaryKeySelective(member);
    }
    //分页显示coach
    @Override
    public PageInfo splitCoach(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = coachMapper.selectByExample();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public TMember selectByKey(Integer key) {
        return memberMapper.selectByPrimaryKey(key);
    }

    @Override
    public Map findCourseMap(List<TApply> list) {
        GetAWeekCourse ga = new GetAWeekCourse();
        return ga.getAWeek(list);
    }

    //2.查询所有教练
    @Override
    public List selectAll() {
        List list = coachMapper.selectByExample();
        return list;
    }
    //会员用户 多条件 查询教练
    @Override
    public PageInfo memFineCoach(int pageNum, int pageSize,String cotype, String coachname) {
        PageHelper.startPage(pageNum,pageSize);
        List list = coachMapper.selectByDireName(cotype,coachname);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
    @Override
    public TCoach findBycoachId(Integer coachid) {
        return coachMapper.selectByPrimaryKey(coachid);
    }

    //查询状态为报名中的所有课程
    @Override
    public PageInfo findAllCourse1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        TCourseExample example = new TCourseExample();
        List list = courseMapper.selectByExample(example);
        PageInfo info = new PageInfo(list);
        return info;
    }
    //多条件查询
    @Override
    public PageInfo findByClause(int pageNum, int pageSize,Float minMoney, Float maxMoney, String coursename) {
        PageHelper.startPage(pageNum,pageSize);
        List list = courseMapper.selectByMoneyName(minMoney,maxMoney,coursename);
        PageInfo info = new PageInfo(list);
        return info;
    }

    //根据id查询课程
    @Override
    public TCourse findBycourseId(Integer courseId) {
        return courseMapper.selectByPrimaryKey(courseId);
    }

    //根据会员号，课程号查询会员是否报名此课程
    @Override
    public TApply findByMemCourse(Integer applymem, Integer applycourse) {
        return applyMapper.selectByMemCourse(applymem,applycourse);
    }

    //插入报名信息
    @Override
    public int addApply(Integer applymem, Integer applycourse) {
        TApply apply = new TApply();
        apply.setApplymem(applymem);
        apply.setApplycourse(applycourse);
        apply.setApplystates("1");
        return applyMapper.insertSelective(apply);
    }

    //根据用户id查询用户报名的课程(未结束的课程)，展示在个人中心课程报名信息模块中
    @Override
    public List findAllcourse(Integer memid) {

        return applyMapper.selectAllCourse(memid);
    }

    //根据用户id查询用户报名的课程(未结束的课程)，展示在个人中心课程报名信息模块中
    @Override
    public List findAllcourseNoEnd(Integer memid) {

        return applyMapper.selectAllCourseNoEnd(memid);
    }
    @Override
    public List findAllCoach(Integer memid) {
        return reserveMapper.selectAllCoach(memid);
    }

    @Override
    public int addReserve(Integer resermem,Integer resercoach) {
        TReserve reserve = new TReserve();
        reserve.setReservemem(resermem);
        reserve.setReservecoach(resercoach);
        reserve.setReservestates("1");
        return reserveMapper.insertSelective(reserve);
    }

    @Override
    public TReserve findByMemCoa(Integer memid, Integer coachid) {
        return reserveMapper.selectByMemCoa(memid,coachid);
    }

    @Override
    public TReserve findByResId(Integer reserveid) {
        return reserveMapper.selectByPrimaryKey(reserveid);
    }

    @Override
    public int editDate(TReserve reserve) {
        return reserveMapper.updateByPrimaryKeySelective(reserve);
    }

    @Override
    public int countMem(Integer courseId) {
        return applyMapper.countMem(courseId);
    }
}
